﻿using System.IO;
using System.Runtime.InteropServices;
using BestHTTP;
using UnityEngine;

namespace Networking.Runtime
{
    public static class DownLoadSetting
    {
        public static bool UseThreadUpdate
        {
            get => HTTPUpdateDelegator.IsThreaded;
            set => HTTPUpdateDelegator.IsThreaded = value;
        }

        /// <summary>
        /// 长度
        /// </summary>
        public const string HEADER_CONTENT_LENGTH = "content-length";

        public const int DOWN_LOAD_THREAD_COUNT = 4;

        /// <summary>
        /// 每次下载超时后 重新下载自动叠加的时间
        /// </summary>
        public const int DEL_ADD_TIME_OUT = 60 * 10;

        /// <summary>
        ///  初始值 下载超时时间
        /// </summary>
        public const int DOWNLOAD_TIME_OUT = 60 * 60 * 4;

        /// <summary>
        /// 初始值 下载链接超时时间
        /// </summary>
        public const int CONNECT_TIMEOUT = 60;

        public const int MAX_RETRIES = 3;
        public const int MAX_FRAME_DOWNLOAD = 10 * 1024 * 1024;

        public static readonly string DIR_DOWNLOAD_TEMP = Path.Combine(COOKIES_PATH, "dowloadCaches");
#if UNITY_EDITOR || UNITY_WINDOWS
        public static string COOKIES_PATH => Application.dataPath;
#else
        public static  string COOKIES_PATH  => Application.persistentDataPath;
#endif

#if UNITY_IOS
        [DllImport("__Internal")]
        private static extern void backgroundLaunch();

        [DllImport("__Internal")]
        private static extern void backgroundStop();
#endif

        // Start background task
        public static void StartTask()
        {
#if UNITY_EDITOR
#elif UNITY_IOS
	     backgroundLaunch ();
#endif
        }

        // Stop background task
        public static void StopTask()
        {
#if UNITY_EDITOR
#elif UNITY_IOS
             backgroundStop ();
#endif
        }
    }
}